<?php
require_once('./db.php');

$sql = "SELECT * FROM `invoices` WHERE `next_issue` = CURDATE() AND `period` > 0";
$q = $db->runSQL($sql);

while ($job = $db->loadObject($q)) {
	$update = "UPDATE `invoices` SET `count` = (`count`+1), `next_issue` = DATE_ADD(`next_issue`, INTERVAL `period` MONTH) WHERE `id` = ".$job->id;
	$db->runSQL($update);
	
	$invoiceSql = "INSERT INTO `ainvoices` (`jid`, `type`, `date`, `partial`, `fields`, `proforma`) VALUES (".$job->id.", 1, CURDATE(), 0, '', 0)";
	$db->runSQL($invoiceSql);
}

?>